package com.yc.music.utils;

import ohos.agp.components.ComponentProvider;
import ohos.agp.utils.Color;
import ohos.app.AbilityContext;
import ohos.app.Context;
import ohos.event.intentagent.IntentAgent;
import ohos.event.notification.NotificationHelper;
import ohos.event.notification.NotificationRequest;
import ohos.event.notification.NotificationSlot;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.rpc.RemoteException;
import ohos.utils.net.Uri;

import java.io.IOException;

/**
 * 通知栏工具类
 *
 * @since 2021-05-10
 */
public class NotificationUtils extends AbilityContext {
    /**
     * CHANNEL_ID
     */
    public static final String CHANNEL_ID = "default";
    /**
     * isVibration
     */
    private static boolean isVibration = false;
    private static final String CHANNEL_NAME = "Default_Channel";
    private final Context mContext;
    private NotificationSlot mSlot;

    private boolean ongoing = false;
    private ComponentProvider componentProvider = null;
    private IntentAgent intent = null;
    private String ticker;
    private int priority = NotificationSlot.LEVEL_DEFAULT;
    private boolean isOnlyAlertOnce = false;
    private long when = 0L;
    private Uri sound = null;
    private long[] pattern = null;

    /**
     * NotificationUtils
     *
     * @param base base
     */
    public NotificationUtils(Context base) {
        super(base);
        mContext = base;
        createNotificationChannel();
    }

    public static boolean isIsVibration() {
        return isVibration;
    }

    public static void setIsVibration(boolean isVibration) {
        NotificationUtils.isVibration = isVibration;
    }

    private void createNotificationChannel() {
        // 第一个参数：channel_id
        // 第二个参数：channel_name
        // 第三个参数：设置通知重要性级别
        // 注意：该级别必须要在 NotificationChannel 的构造函数中指定，总共要五个级别；
        // 范围是从 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)
        mSlot = new NotificationSlot(CHANNEL_ID, CHANNEL_NAME,
            NotificationSlot.LEVEL_DEFAULT);
        mSlot.isEnableBypassDnd(); // 是否绕过请勿打扰模式
        mSlot.setEnableLight(true); // 闪光灯
        mSlot.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_SECRET); // 锁屏显示通知
        mSlot.setLedLightColor(Color.RED.getValue()); // 闪关灯的灯光颜色
        mSlot.isShowBadge(); // 桌面launcher的消息角标
        mSlot.setEnableVibration(isVibration); // 是否允许震动
        // 获取系统通知响铃声音的配置
        mSlot.getSlotGroup(); // 获取通知取到组
        mSlot.enableBypassDnd(true); // 设置可绕过 请勿打扰模式
        mSlot.setVibrationStyle(new long[]{100, 100, 200}); // 设置震动模式
        mSlot.canEnableLight(); // 是否会有灯光
        mSlot.setLevel(priority);
        try {
            NotificationHelper.addNotificationSlot(mSlot);
        } catch (RemoteException e) {
            MusicLogUtils.e(e.getMessage());
        }
    }

    /**
     * 获取创建一个NotificationManager的对象
     *
     * @return NotificationManager对象
     */
    public NotificationHelper getManager() {
        return null;
    }

    /**
     * 清空所有的通知
     */
    public void clearNotification() {
        try {
            NotificationHelper.cancelAllNotifications();
        } catch (RemoteException e) {
            MusicLogUtils.e(e.getMessage());
        }
    }

    /**
     * 获取Notification
     *
     * @param title title
     * @param content content
     * @param icon icon
     * @return return
     */
    public NotificationRequest getNotification(String title, String content, int icon) {
        return getChannelNotification(title, content, icon);
    }

    /**
     * 建议使用这个发送通知
     * 调用该方法可以发送通知
     *
     * @param notifyId
     * @param title
     * @param content
     * @param icon
     */
    public void sendNotification(int notifyId, String title, String content, int icon) {
        getChannelNotification(title, content, icon);
        getManager().notify();
    }

    private NotificationRequest getChannelNotification(String title, String content, int icon) {
        NotificationRequest.NotificationNormalContent builder = new NotificationRequest.NotificationNormalContent();

        builder.setTitle(title) // 设置标题
            .setText(content); // 消息内容
        NotificationRequest notificationBuilder = new NotificationRequest();

        Resource imageResource = null;
        try {
            imageResource = mContext.getResourceManager().getResource(icon);
            PixelMap pixelmap = ImageSource.create(imageResource, null).createPixelmap(null);
            notificationBuilder.setLittleIcon(pixelmap) // 设置通知的图标

                // 让通知左右滑的时候是否可以取消通知
                .setInProgress(ongoing)

                // 是否提示一次.true - 如果Notification已经存在状态栏即使在调用notify函数也不会更新
                .setAlertOneTime(isOnlyAlertOnce)
                .setTapDismissed(true)
                .setSlotId(CHANNEL_ID);
        } catch (IOException | NotExistException e) {
            MusicLogUtils.e(e.getMessage());
        }
        if (componentProvider != null) {
            // 设置自定义view通知栏
            notificationBuilder.setCustomView(componentProvider);
        }
        if (intent != null) {
            notificationBuilder.setIntentAgent(intent);
        }
        if (ticker != null && ticker.length() > 0) {
            // 设置状态栏的标题
            notificationBuilder.setStatusBarText(ticker);
        }
        if (when != 0) {
            // 设置通知时间，默认为系统发出通知的时间，通常不用设置
            notificationBuilder.setDeliveryTime(when);
        }
        if (sound != null) {
            // 设置sound
            mSlot.setSound(sound);
        }
        if (pattern != null) {
            // 自定义震动效果
            mSlot.setVibrationStyle(pattern);
        }
        NotificationRequest.NotificationContent notification = new NotificationRequest.NotificationContent(builder);
        notificationBuilder.setContent(notification);
        return notificationBuilder;
    }

    /**
     * 让通知左右滑的时候是否可以取消通知
     *
     * @param ongoing 是否可以取消通知
     * @return return
     */
    public NotificationUtils setOngoing(boolean ongoing) {
        this.ongoing = ongoing;
        return this;
    }

    /**
     * 设置自定义view通知栏布局
     *
     * @param provider view
     * @return return
     */
    public NotificationUtils setContent(ComponentProvider provider) {
        this.componentProvider = provider;
        return this;
    }

    /**
     * 设置内容点击
     *
     * @param intent intent
     * @return return
     */
    public NotificationUtils setContentIntent(IntentAgent intent) {
        this.intent = intent;
        return this;
    }

    /**
     * 设置状态栏的标题
     *
     * @param ticker 状态栏的标题
     * @return return
     */
    public NotificationUtils setTicker(String ticker) {
        this.ticker = ticker;
        return this;
    }

    /**
     * 设置优先级
     *
     * @param priority 优先级，默认是Notification.PRIORITY_DEFAULT
     * @return return
     */
    public NotificationUtils setPriority(int priority) {
        this.priority = priority;
        return this;
    }

    /**
     * 是否提示一次.true - 如果Notification已经存在状态栏即使在调用notify函数也不会更新
     *
     * @param onlyAlertOnce 是否只提示一次，默认是false
     * @return NotificationUtils
     */
    public NotificationUtils setOnlyAlertOnce(boolean onlyAlertOnce) {
        this.isOnlyAlertOnce = onlyAlertOnce;
        return this;
    }

    /**
     * 设置通知时间，默认为系统发出通知的时间，通常不用设置
     *
     * @param when when
     * @return return
     */
    public NotificationUtils setWhen(long when) {
        this.when = when;
        return this;
    }

    /**
     * 设置sound
     *
     * @param sound sound
     * @return return
     */
    public NotificationUtils setSound(Uri sound) {
        this.sound = sound;
        return this;
    }

    /**
     * 设置默认的提示音
     *
     * @param defaults defaults
     * @return return
     */
    public NotificationUtils setDefaults(int defaults) {
        return this;
    }

    /**
     * 自定义震动效果
     *
     * @param pattern pattern
     * @return return
     */
    public NotificationUtils setVibrate(long[] pattern) {
        this.pattern = pattern.clone();
        return this;
    }

    /**
     * 设置flag标签
     *
     * @param flags flags
     * @return return
     */
    public NotificationUtils setFlags(int... flags) {
        return this;
    }
}

